package boot;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebAppInitializer implements WebApplicationInitializer {

	@Override
	public void onStartup(ServletContext container) throws ServletException {

		container.setInitParameter("spring.profiles.active", "jpa");

		// Creates the root application context
		AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
		appContext.register(RootConfig.class);
		container.addListener(new ContextLoaderListener(appContext));

		// Registers the servlet configuraton with the dispatcher servlet
		// context
		AnnotationConfigWebApplicationContext servletContext = new AnnotationConfigWebApplicationContext();

		// Registers the servlet configuraton with the dispatcher servlet
		// context
		servletContext.register(MvcConfig.class);

		// Further configures the servlet context
		ServletRegistration.Dynamic dispatcher = container.addServlet("admin", new DispatcherServlet(servletContext));
		dispatcher.setLoadOnStartup(1);
		dispatcher.addMapping("/admin/*");
	}
}
